package com.mk.service.web;

import act.controller.Controller;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.ApiMap;
import com.github.d925529.apidoc.annotation.ApiMethod;
import com.github.d925529.apidoc.annotation.ApiParam;
import com.github.d925529.apidoc.annotation.ApiReturn;
import com.mk.domain.TProduct;
import com.mk.domain.TProductSpec;
import com.mk.enums.GlobalValues;
import jdk.nashorn.internal.objects.Global;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("tProduct")
public class TProductService extends BaseWebService {
    @Inject
    private TProduct.Mapper tProductMapper;
    @Inject
    private TProductSpec.Mapper tProductSpecMapper;
    @Inject
    private TOrderService tOrderService;

    @With(WbILogin.class)
    @GetAction("list.json")
    @ApiMethod(title = "查询指定类型的活动列表", path = "/list.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "pdname", title = "商品名称", type = String.class)
    @ApiParam(name = "pddesc", title = "商品描述", type = String.class)
    @ApiParam(name = "bdate", title = "开始时间", type = String.class)
    @ApiParam(name = "edate", title = "结束时间", type = String.class)
    @ApiParam(name = "brid", title = "系列id", type = String.class)
    @ApiParam(name = "brname", title = "系列名称", type = String.class)
    @ApiParam(name = "curpage", title = "当前页数，从0开始", type = Integer.class)
    @ApiParam(name = "pagesize", title = "每页条数", type = Integer.class)
    @ApiReturn(description = "商品及skuMap", type = List.class, elementType = Map.class, maps={
            @ApiMap(name = "pdid", title = "商品id...", type = String.class),
    })
    public List<Map<String,Object>> productList(String pdname, String pddesc,
                                                String bdate, String edate,
                                                String brid, String brname,
                                                Integer curpage, Integer pagesize) {
        PageQuery query = new PageQuery();
        query.setPageSize(pagesize);
        query.setPageNumber(curpage+1);
        Map<String, Object> map = new HashMap<>();
        map.put("pdname", pdname);
        map.put("pddesc", pddesc);
        map.put("bdate",bdate);
        map.put("edate",edate);
        map.put("brid",brid);
        map.put("brname",brname);
        query.setParas(map);
        //此处的sql是三个sql
        tProductMapper.getSQLManager().pageQuery("tProduct.product_list", Map.class, query);
        return query.getList();
    }


    @With(WbILogin.class)
    @GetAction("detail.json")
    @ApiMethod(title = "查询指定类型的活动列表", path = "/list.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "pdid", title = "商品主键", type = String.class)
    @ApiReturn(description = "商品及skuMap", type = List.class, elementType = TProduct.class)
    public TProduct productDetail(String pdid) {
        TProduct product = tProductMapper.single(pdid);
        product.setDiscount(tOrderService.getDiscount());
        if(product!=null){
            TProductSpec param = new TProductSpec();
            param.setPdid(pdid);
            List<TProductSpec> list = tProductSpecMapper.template(param);
            product.setSkuList(list);
        }
        return product;
    }

    @With(WbILogin.class)
    @GetAction("productcount.json")
    public Long getProductCount(){
        TProduct product = new TProduct();
        product.setPdstate(GlobalValues.Z_PD_STATE_3.value());
        return tProductMapper.templateCount(product);
    }
}
